package com.baomidou.springwind.controller;

import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.entity.Department;
import com.baomidou.springwind.service.IDepartmentService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by Administrator on 2017/3/7.
 */


@RequestMapping("/department")
@Controller
public class DepartmentController extends BaseController {
    @Autowired
    private IDepartmentService iDepartmentService;

    @Permission("6001")
    @RequestMapping("/list")
    public String list(Model model) {
        return "/department/list";
    }

    @Permission("6001")
    @ResponseBody
    @RequestMapping("/getDepartmentList")
    public String getDepartmentList() {
        Page<Department> page = getPage();
        Page<Department> departmentPage = iDepartmentService.selectPage(page, null);
        return jsonPage(departmentPage);
    }

    @Permission("6001")
    @RequestMapping("/edit")
    public String editDepartment(Model model, String id) {
        if (id != null) {
            Department department = iDepartmentService.selectById(id);
            model.addAttribute("department", department);
        }
        return "/department/edit";
    }

    @Permission("6001")
    @ResponseBody
    @RequestMapping("/updateDepartment")
    public String updateDepartment(Department department) {
        boolean rtl = false;
        Date date = new Date();
        department.setChangetime(DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss"));
        rtl = iDepartmentService.upDateDepartmentById(department);
        return callbackSuccess(rtl);
    }


    @Permission("6001")
    @ResponseBody
    @RequestMapping("/deleteDepartment/{id}")
    public String deleteDepartment(@PathVariable String id) {
        Boolean rtl = false;
        if (id != null) {
            rtl = iDepartmentService.deleteById(id);
        }
        return rtl.toString();
    }


}
